using System;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Collections;
using System.Net;
using Transferomatic.Views;
using System.Diagnostics;
using System.Drawing;
using System.Deployment.Application;
using System.ComponentModel;
using Transferomatic.Properties;
using Transferomatic.Enumerators;

namespace Transferomatic.ApplicationControllers
{
    public partial class Controller
    {
        #region Methods
        private void DirectoryTransferView_Show(
            MainView theParentView)
        {
            // Instantiate an instance
            DirectoryTransferView theView = new DirectoryTransferView();

            theView.buttonOpenSource.Click += 
                new EventHandler(DirectoryTransferView_buttonOpenSource_Click);
            theView.buttonOpenDestination.Click += 
                new EventHandler(DirectoryTransferView_buttonOpenDestination_Click);
            theView.buttonOK.Click += 
                new EventHandler(DirectoryTransferView_buttonOK_Click);

            theView.folderbrowserdialogSource.SelectedPath =
                RegistryAgent.LastDirectoryTransferSourceDirectory;
            theView.folderbrowserdialogDestination.SelectedPath =
                theView.textboxDestination.Text = 
                    RegistryAgent.LastDirectoryTransferDestinationDirectory;

            // Show the form
            theView.ShowDialog(theParentView);
        }

        private void DirectoryTransferView_Show(
            MainView theParentView,
            TransferAgent theAgent)
        {
            TransferAgent theTransferAgent =
                theAgent as TransferAgent;

            // Instantiate an instance
            DirectoryTransferView theView = new DirectoryTransferView();

            theView.buttonOpenSource.Click += 
                new EventHandler(DirectoryTransferView_buttonOpenSource_Click);
            theView.buttonOpenDestination.Click += 
                new EventHandler(DirectoryTransferView_buttonOpenDestination_Click);
            theView.buttonOK.Click += 
                new EventHandler(DirectoryTransferView_buttonOK_Click);

            theView.Tag = theAgent;

            theView.folderbrowserdialogSource.SelectedPath =
                theView.textboxSource.Text =
                    theTransferAgent.SourceFiles[0].FullName;
            theView.folderbrowserdialogDestination.SelectedPath =
                theView.textboxDestination.Text =
                    theTransferAgent.Destination.FullName;

            // Show the form
            theView.ShowDialog(theParentView);
        }
        
        #region Event Handlers
        private void DirectoryTransferView_buttonOpenSource_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theOpenSourceButton =
                sender as Button;
            DirectoryTransferView theParentView =
                theOpenSourceButton.FindForm() as DirectoryTransferView;

            switch (theParentView.folderbrowserdialogSource.ShowDialog())
            {
                case DialogResult.Cancel:
                    Application.DoEvents();
                    return;
            }

            theParentView.textboxSource.Text =
                theParentView.folderbrowserdialogSource.SelectedPath;

            theParentView.buttonOK.Enabled =
                !String.IsNullOrEmpty(theParentView.textboxSource.Text) &&
                !String.IsNullOrEmpty(theParentView.textboxDestination.Text);
        }

        private void DirectoryTransferView_buttonOpenDestination_Click(
            object sender, 
            EventArgs e)
        {
            Application.DoEvents();

            Button theOpenDestinationButton =
                sender as Button;
            DirectoryTransferView theParentView =
                theOpenDestinationButton.FindForm() as DirectoryTransferView;

            switch (theParentView.folderbrowserdialogDestination.ShowDialog())
            {
                case DialogResult.Cancel:
                    Application.DoEvents();
                    return;
            }

            theParentView.textboxDestination.Text =
                theParentView.folderbrowserdialogDestination.SelectedPath;

            theParentView.buttonOK.Enabled =
                !String.IsNullOrEmpty(theParentView.textboxSource.Text) &&
                !String.IsNullOrEmpty(theParentView.textboxDestination.Text);
        }

        private void DirectoryTransferView_buttonOK_Click(
            object sender, 
            EventArgs e)
        {
            Button theOKButton =
                sender as Button;
            DirectoryTransferView theParentView =
                theOKButton.FindForm() as DirectoryTransferView;
            TransferAgent theAgent =
                theParentView.Tag as TransferAgent;

            Application.DoEvents();

            theParentView.Cursor = Cursors.WaitCursor;

            RegistryAgent.LastDirectoryTransferSourceDirectory =
                theParentView.textboxSource.Text;
            RegistryAgent.LastDirectoryTransferDestinationDirectory =
                theParentView.textboxDestination.Text;

            if (null != theAgent)
            {
                for (int i = this._MainView.gridTransfers.Rows.Count - 1; i > -1; i--)
                {
                    TransferAgent thisAgent =
                        this._MainView.gridTransfers.Rows[i].Tag
                            as TransferAgent;

                    if (thisAgent.ID.Equals(theAgent.ID))
                    {
                        this._MainView.gridTransfers.Rows.RemoveAt(i);
                    }
                }
            }

            TransferAgent theNewAgent = new TransferAgent(
                TransferAgentType.Folder,
                theParentView.Action,
                RegistryAgent.BufferSize,
                new DirectoryInfo(theParentView.textboxSource.Text),
                new DirectoryInfo(theParentView.textboxDestination.Text));

            theNewAgent.Transferring +=
                new EventHandler(TransferAgent_Transferring);
            theNewAgent.Paused +=
                new EventHandler(TransferAgent_Paused);
            theNewAgent.Failed +=
                new EventHandler(TransferAgent_Failed);
            theNewAgent.Completed +=
                new EventHandler(TransferAgent_Completed);
            theNewAgent.Cancelled +=
                new EventHandler(TransferAgent_Cancelled);
            theNewAgent.StatusChanged +=
                new EventHandler(TransferAgent_StatusChanged);

            ExtendedDialogResult theExtendedDialogResult =
                ExtendedDialogResult.NotSet;

            this.BuildSources(
                theNewAgent,
                new DirectoryInfo(theParentView.textboxSource.Text),
                new DirectoryInfo(theParentView.textboxDestination.Text),
                theExtendedDialogResult);

            if (theNewAgent.SourceFiles.Count.Equals(0))
            {
                MessageBox.Show(
                    theParentView,
                    "The directories are identical",
                    theParentView.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information,
                    MessageBoxDefaultButton.Button1);

                Application.DoEvents();
            }
            else
            {
                int thisIndex =
                     this._MainView.gridTransfers.Rows.Add(
                        Resources.folder,
                        theNewAgent.Action,
                        theNewAgent.SourceDescription,
                        theNewAgent.Destination.FullName,
                        String.Empty,
                        "0%",
                        String.Empty);

                this._MainView.gridTransfers.Rows[thisIndex].Tag = theNewAgent;

                this._MainView.gridTransfers.ClearSelection();

                this.UpdateControlButtons();
            }

            theParentView.Cursor = Cursors.Default;

            theParentView.Close();
        }
        #endregion

        #region Private Helpers
        private void BuildSources(
            TransferAgent theNewAgent,
            DirectoryInfo theSourceDirectoryInfo,
            DirectoryInfo theDestinationDirectoryInfo,
            ExtendedDialogResult theExtendedDialogResult)
        {
            foreach (FileInfo thisSourceFileInfo in
                theSourceDirectoryInfo.GetFiles())
            {
                FileInfo thisDestinationFileInfo = new FileInfo(
                    theDestinationDirectoryInfo + @"\" + thisSourceFileInfo.Name);

                if (thisDestinationFileInfo.Exists)
                {
                    if (theExtendedDialogResult.Equals(ExtendedDialogResult.NotSet) ||
                        theExtendedDialogResult.Equals(ExtendedDialogResult.Ignore) ||
                        theExtendedDialogResult.Equals(ExtendedDialogResult.Overwrite))
                    {
                        theExtendedDialogResult =
                            this.ConfirmationDialogView_Show(
                                this._MainView,
                                String.Format(
                                    CultureInfo.CurrentCulture,
                                    "File '{0}' already exists with a size of {1}Mb\n\n" +
                                    "What do you want to do?",
                                    thisDestinationFileInfo.FullName,
                                    Math.Round((decimal)thisDestinationFileInfo.Length / 1048576, 2)));

                        Application.DoEvents();
                    }

                    switch (theExtendedDialogResult)
                    {
                        case ExtendedDialogResult.Overwrite:
                        case ExtendedDialogResult.OverwriteAll:
                            thisDestinationFileInfo.Delete();
                            break;
                        case ExtendedDialogResult.Ignore:
                        case ExtendedDialogResult.IgnoreAll:
                            continue;
                    }
                }

                theNewAgent.SourceFiles.Add(thisSourceFileInfo);
            }

            foreach (DirectoryInfo thisSourceSubDirectoryInfo in
                theSourceDirectoryInfo.GetDirectories())
            {
                DirectoryInfo theDestinationSubDirectoryInfo = new DirectoryInfo(
                    theDestinationDirectoryInfo.FullName + @"\" + thisSourceSubDirectoryInfo.Name);

                this.BuildSources(
                    theNewAgent,
                    thisSourceSubDirectoryInfo,
                    theDestinationSubDirectoryInfo,
                    theExtendedDialogResult);
            }
        }
        #endregion
        #endregion
    }
}
